﻿/**
 * @file tipwindow.cpp
 * @author huangxb
 * @email 18228033207@163.com
 * @date 2025-07-09
 * @brief 用于显示提示信息的独立小弹窗
 */
#include "tipwindow.h"
#include "ui_tipwindow.h"
#include <QTimer>
#include <chrono>

QQueue<QPointer<CTipWindow>> CTipWindow::m_queueShownTipWindow;

CTipWindow::CTipWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CTipWindow)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_DeleteOnClose);
    setAttribute(Qt::WA_StyledBackground);
    setAttribute(Qt::WA_TranslucentBackground);
}

CTipWindow::~CTipWindow()
{
    delete ui;
}

void CTipWindow::ShowSuccessTip(QPoint pt, const QString &str, QWidget *parent, int secs)
{
    CTipWindow* pWindow = new CTipWindow(parent);
    pWindow->SetSuccessTip(str);

    std::chrono::seconds sec(secs);
    std::chrono::milliseconds misc(sec);
    pWindow->Show(pt, misc.count());
}

void CTipWindow::closeEvent(QCloseEvent *event)
{
    QWidget::closeEvent(event);

    while(!m_queueShownTipWindow.empty())
    {
        if(m_queueShownTipWindow.front().isNull())
        {
            m_queueShownTipWindow.pop_front();
        }
        else
        {
            break;
        }
    }
}

void CTipWindow::SetSuccessTip(const QString &str)
{
    setStyleSheet(R"(
#centralWidget{border-radius: 8px;background: white;}
#widget{border-radius: 8px;border: 1px solid #36A95C;background: rgba(54,169,92,0.25);}
#m_icon_label{image:url(:/resources/image/icon_success_tip.svg);}
#m_tip_label{font-size:9pt;}
)");
    ui->m_tip_label->setText(str);
}

void CTipWindow::Show(QPoint pt, int misc)
{
    adjustSize();
    // 如果有正在显示的tip，为避免重合，需要重新计算位置
    if(!m_queueShownTipWindow.empty() && !m_queueShownTipWindow.back().isNull())
    {
        auto rcLast = m_queueShownTipWindow.back()->geometry();
        pt.setY(rcLast.bottom() + 11);
    }
    move(pt);
    show();
    m_queueShownTipWindow.push_back(this);
    QTimer::singleShot(misc, this, &CTipWindow::close);
}
